#include "CTableUser.h"

CTableUser::CTableUser(QObject *parent)
    : AbstractTable{"User",parent}
{
    QString createSql = "CREATE TABLE User ("
            "Id            INTEGER PRIMARY KEY"
            "                      AUTOINCREMENT"
            "                      NOT NULL,"
            "Role          INTEGER NOT NULL,"
            "UserName      TEXT    UNIQUE"
            "                      NOT NULL,"
            "Active        BOOLEAN NOT NULL"
            "                      DEFAULT (1),"
            "Locked        BOOLEAN NOT NULL"
            "                      DEFAULT (0),"
            "LoginType     INTEGER NOT NULL,"
            "LastLoginTime TEXT"
            ");";

    if(!exists()){
        createTable(createSql);
    }
}

CTableUser *CTableUser::getInstance()
{
    static CTableUser* pInstance = nullptr;
    if(nullptr == pInstance){
        pInstance = new CTableUser();
    }
    return pInstance;
}

bool CTableUser::addUser(const CUserInfo &info)
{
    QVariantMap map;
    map.insert("Role",info.role);
    map.insert("UserName",info.userName);
    map.insert("Active",info.active);
    map.insert("Locked",info.locked);
    map.insert("LoginType",info.loginType);
    map.insert("LastLoginTime",info.lastLoginTime);

    bool ok = AbstractTable::addRow(map);

    return ok;
}

bool CTableUser::deleteUser(const QString &userId)
{
    bool ok = AbstractTable::deleteRow("Id",userId);
    return ok;
}

bool CTableUser::updateUser(const CUserInfo &info)
{
    QVariantMap map;
    map.insert("Role",info.role);
    map.insert("UserName",info.userName);
    map.insert("Active",info.active);
    map.insert("Locked",info.locked);
    map.insert("LoginType",info.loginType);
    map.insert("LastLoginTime",info.lastLoginTime);

    bool ok = AbstractTable::updateRow("Id",info.userId,map);

    return ok;
}

QSharedPointer<CUserInfo> CTableUser::userInfo(const QString &userId)
{
    QSharedPointer<QVariantMap> map = AbstractTable::queryRow("Id",userId);

    qDebug() << "userInfo map:" << map;
    QSharedPointer<CUserInfo> info(new CUserInfo());
    info->userId = map->value("Id").toString();
    info->role = map->value("Role").toInt();
    info->userName = map->value("UserName").toString();
    info->active = map->value("Active").toBool();
    info->locked = map->value("Locked").toBool();
    info->loginType = map->value("LoginType").toInt();
    info->lastLoginTime = QDateTime::fromString(map->value("LastLoginTime").toString());

    return info;
}

QSharedPointer<QList<CUserInfo>> CTableUser::allUser()
{
    QSharedPointer<QList<CUserInfo>> listUser(new QList<CUserInfo>);

    QSharedPointer<QList<QVariantMap>> listMap = AbstractTable::queryAll();

    foreach (QVariantMap map,*listMap) {
        qDebug() << "allUser map:" << map;
        CUserInfo info;
        info.userId = map["Id"].toString();
        info.role = map["Role"].toInt();
        info.userName = map["UserName"].toString();
        info.active = map["Active"].toBool();
        info.locked = map["Locked"].toBool();
        info.loginType = map["LoginType"].toInt();
        info.lastLoginTime = QDateTime::fromString(map["LastLoginTime"].toString());

        qDebug() << "all user one:" << info;
        listUser->append(info);
    }


    return listUser;
}
